package org.example.springblogg.service;

import lombok.extern.slf4j.Slf4j;
import org.example.springblogg.mapper.BlogMapper;
import org.example.springblogg.mapper.UserInfoMapper;
import org.example.springblogg.model.BlogInfo;
import org.example.springblogg.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlogMapper blogMapper;

    public UserInfo selectByName(String userName){
       return  userInfoMapper.selectByName(userName);
    }

    public UserInfo selectById(Integer userId){
        return userInfoMapper.selectUserById(userId);
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        BlogInfo blogInfo = blogMapper.selectById(blogId);
        if(blogInfo == null || blogInfo.getUserId() <= 0){
            log.error("图书不存在或者博客ID不合法: blogId:{}"+blogId);
            return null;
        }
        return userInfoMapper.selectUserById(blogInfo.getUserId());
    }
}
